package com.example.onlinemusic.controller;

import com.example.onlinemusic.mapper.LoveMusicMapper;
import com.example.onlinemusic.mapper.MusicMapper;
import com.example.onlinemusic.model.Music;
import com.example.onlinemusic.model.User;
import com.example.onlinemusic.tools.Constant;
import com.example.onlinemusic.tools.ResponseBodyMessage;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/lovemusic")
public class LoveMusicController {

    @Autowired//类前边不加@RestController，这里会报错
    private LoveMusicMapper loveMusicMapper;
    @Autowired
    private MusicMapper musicMapper;

    @RequestMapping("/likemusic")
    public ResponseBodyMessage<Boolean> likemusic( @RequestParam int musicId, HttpServletRequest request) {


        //1.先检查用户是否登录
        HttpSession httpSession = request.getSession(false);
        if (httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("收藏失败，用户尚未登录");
            return new ResponseBodyMessage<>(-1, "请先登录", false);
        }

        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();

        Music music = loveMusicMapper.findLoveMusic(userId, musicId);
        if (music != null) {
            System.out.println("已经收藏过该音乐");
            if(loveMusicMapper.updateLoveMusic(userId, musicId))
            return new ResponseBodyMessage<>(-1,"已经收藏过该音乐，本次请求下，已经取消收藏",false);
        }

        if(loveMusicMapper.insertLoveMusic(userId,musicId)){
            System.out.println("收藏成功");
            return new ResponseBodyMessage<>(0,"收藏成功",true);
        }

        return new ResponseBodyMessage<>(-1,"收藏失败",false);

    }


    @RequestMapping("/findlovemusic")
    public ResponseBodyMessage<List<Music>> findLoveMusic(@RequestParam(required = false) String musicName, HttpServletRequest request) {
        //1.先判断是否登录（跟前边的逻辑一样）
          //“1.先检查用户是否登录
        HttpSession httpSession = request.getSession(false);
        if (httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("查询失败，用户尚未登录");
            return new ResponseBodyMessage<>(-1, "查询失败，请先登录", null);
        }

        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();


        //2.登录了，看参数（有无）
        List<Music> musicList = null;
        if(musicName != null){
            //2.1有参数-方法：
             musicList = loveMusicMapper.findLoveMusicByMusicName(musicName,userId);
        }else {
            //2.2无参数-方法：
            musicList = loveMusicMapper.findLoveMusicByUserId(userId);
        }

        return new ResponseBodyMessage<>(0,"查找到了所有的音乐",musicList);

    }

    @RequestMapping("/deletlovemusic")
    public ResponseBodyMessage<Boolean> deletLoveMusic(@RequestParam String id, HttpServletRequest request){
        //1.判断是否登录
        HttpSession httpSession = request.getSession(false);
        if (httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("操作失败，请先登录");
            return new ResponseBodyMessage<>(-1,"操作失败，请先登录",false);
        }

        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();

        //2.取消收藏
        int musicId = Integer.parseInt(id);
        int a = loveMusicMapper.deleteLoveMusic(userId,musicId);
        if(a == 1){
            System.out.println("取消收藏成功！");
            return new ResponseBodyMessage<>(0,"取消收藏成功！",true);
        }
        return new ResponseBodyMessage<>(-1,"取消收藏失败！",false);
    }

}
